import {orderList as agentOrderList, paymentByWarehourse,orderSummaryListV2} from '../../../../../api/order'
import { reject, cancel } from '../../../../../api/orderAdmin'
import { getInfo } from '../../../../../api/member'
const util = require('../../../../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 20,
    memberId:'',
    memberName:'',
    detailedList: [],
    orderData:[],
    today:'',
    date:'',
    startDate:'',
    endDate:'',
    VstartDate:'',
    VendDate:'',
    choseData:"",
    tab:[{index:1,title:"今日",fields:'day',checked:true},{index:2,title:"月度",fields:'month'},{index:3,title:"年度",fields:'year'}],
    fields:'day',
    type:'',
    achieve:0,
    count:0,
    currentTab:0,
    amount:0,
    level1Amount:0,
    level2Amount:0,
    level1Progress:0,
    level2Progress:0,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      // console.log("我的业绩页面",options);
      if(options){
        this.data.memberId = options.id
        this.data.memberName = options.name
        this.setData({
          memberName:this.data.memberName,
        })
      }else{
        this.getInfo()
      }
  },

  /**z
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.pageNum=1,
    this.data.pageSize=20,
    this.init()
    this.loadOrder()
    this.loadSummary()
  },
  init(){
    var date =new Date()
    this.data.today = date
    this.data.fields = "day"
    this.data.date = util.datetime(date)
    this.data.startDate = this.data.date
    this.data.endDate = this.data.date
    this.data.icon = util.thumbnail('https://rzico-weex.oss-cn-hangzhou.aliyuncs.com/45082/income.png',80,80)
    this.setData({
      type:"1",
      fields:this.data.fields,
      tab:this.data.tab,
      startDate:this.data.date,
      endDate:this.data.date,
      VstartDate:util.formatData(this.data.fields,date),
      VendDate:util.formatData(this.data.fields,date),
      icon:this.data.icon,
    })
  },
  swichNav: function (e) {
  this.data.pageNum = 1
    // this.data.orderData = []
    var that = this;
    let currTab = e.currentTarget.dataset.current
    if (this.data.currentTab == currTab) {
      return false;
    } else {
      if (currTab == 0) {
        that.setData({
          currentTab: currTab
        })
        this.loadOrder()
      }else if(currTab == 1){
        that.setData({
          currentTab: currTab
        })
        this.loadSummary()
      }
    }
  },
  // 按日期 年度/月度/今日 查询
  tabClick(e){
    let propertyName = "checked"
    this.data.tab.forEach(item=>{
      if(item.index==e.target.id){
        item[propertyName]=true;
        this.data.fields = item.fields
      }else
      item[propertyName]=false
    });
    let data = util.formatData(this.data.fields,this.data.date)
    this.data.choseData = data
    this.data.date = util.datetime(this.data.date)
    this.setData({
      tab:this.data.tab,
      fields:this.data.fields,
      choseData:this.data.choseData,
      date:this.data.date
    })
    this.loadSummary()
  },
    //按日期查询 选择日期
    bindDateChange(e){
      this.data.choseData = util.formatData(this.data.fields,e.detail.value)
      this.data.date = e.detail.value
      this.setData({
        choseData:this.data.choseData,
        date:this.data.date
      })
      this.loadSummary()
    },
    selectStartDate(e){
      this.data.startDate = e.detail.value
      this.data.VstartDate = util.formatData(this.data.fields,e.detail.value)
      this.setData({
        startDate:this.data.startDate,
        VstartDate:this.data.VstartDate,
      })
      this.loadSummary()
    },
    selectEndDate(e){
      this.data.endDate = e.detail.value
      this.data.VendDate = util.formatData(this.data.fields,e.detail.value)
      this.setData({
        endDate:this.data.endDate,
        VendDate:this.data.VendDate,
      })
      this.loadSummary()
    },
  getInfo() {
    var _this = this
    // this.checkLogin(() => {
      getInfo().then(res => {
        if (res.type == 'success') {
          this.data.memberId = res.data.member.id
          _this.setData({
            memberId:this.data.memberId,
            memberName:'',
          })
        }
      }
    ).catch(error => {
    })
    // })
  },

  loadSummary() {
    let _this = this;
    if(util.isNull(this.data.memberId)){
      setTimeout(() => {
        this.getInfo()
      }, 500);
    }
    let list = {
      memberId:this.data.memberId,
      startDate:'',
      endDate:'',
      month: '',
      year:'',
    };
    if(this.data.fields == "day"){
      list.startDate = this.data.startDate;
      list.endDate = this.data.endDate;
    }else if(this.data.fields == "month"){
      list.month = this.data.date.substr(0,7);
    }
    else list.year = this.data.date.substr(0,4)
    orderSummaryListV2(list).then(res => {
      if(res.type =="success"){
        this.data.achieve = res.data.achieve
        this.data.detailedList = res.data.childrens
        if(res.data.childrens){
          res.data.childrens.forEach(item =>{
            if(item.level == 1){
              this.data.level1Amount += item.amount
            }else if(item.level == 2){
              this.data.level2Amount += item.amount
            }
            this.data.amount += item.amount
          })
        }else{
          this.data.level1Amount = 0
          this.data.level2Amount = 0
          this.data.amount = 0
        }
        if(this.data.amount!=0){
          var level1Progress = (Number(this.data.level1Amount)/Number(this.data.amount)*100).toFixed(2)
          var leve2Progress = (Number(this.data.level2Amount)/Number(this.data.amount)*100).toFixed(2)
        }else{
          this.data.level1Progress = 0
          this.data.level2Progress = 0
        }
        _this.setData({
          achieve:this.data.achieve,
          detailedList:this.data.detailedList,
          level1Amount:this.data.level1Amount,
          level2Amount:this.data.level2Amount,
          level1Progress:level1Progress,
          level2Progress:leve2Progress,
          amount:this.data.amount,
        })
      }
    })
  },

  loadOrder() {
    if (this.data.pageNum == 1) {
      this.data.orderData = []
    }
    let params = {}
    params.pageNum = this.data.pageNum
    params.pageSize = this.data.pageSize
    params.memberId = this.data.memberId
    agentOrderList(params).then(res => {
      res.data.data.forEach(item => {
        // console.log(item.createDate);
        item.createDate = util.datetimeFormat(item.createDate) 
        this.data.orderData.push(item)
      })
      this.setData({
        orderData: this.data.orderData
      })
    })
  },
  payForHim(e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '是否确认接单',
      success(res) {
        if (res.confirm) {
          paymentByWarehourse({ orderId: e.currentTarget.dataset.item.id }).then(res => {
            _this.data.orderData.splice(e.currentTarget.dataset.index, 1)
            _this.setData({
              orderData: _this.data.orderData
            })
          })
        }
      }
    })
  },
  cancel(e) {
    wx.showModal({
      title: '您需要关闭该订单嘛？',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon: "none"
          })
        }
        if (res.confirm) {
          cancel(e.currentTarget.dataset.id).then(res => {
            if (res.type == 'success') {
              wx.showToast({
                title: res.content,
                icon: res.type
              })
            }
          }).catch(error => {
            wx.showToast({
              title: error.content,
              icon: "none",
            })
          })
        }
      }
    })
  },
  reject(e) {
    wx.showModal({
      title: '您需要驳回该订单嘛？',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon: "none"
          })
        }
        if (res.confirm) {
          reject(e.currentTarget.dataset.id).then(res => {
            if (res.type == 'success') {
              wx.showToast({
                title: res.content,
                icon: res.type
              })
            }
          }).catch(error => {
            wx.showToast({
              title: error.content,
              icon: "none",
            })
          })
        }
      }
    })
  },
  detailsClick(e) {
    wx.navigateTo({
      url: `/pages/order/jumpPage/order/details?id=${e.currentTarget.dataset.id}&type=read`
    })
  },

    //检测用户的登录状态
    checkLogin(callback) {
      if (app.globalData.token == null) {
        wx.navigateTo({
          url: '/pages/login/index',
        })
      } else {
        if (callback) {
          callback()
        }
      }
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    this.data.pageNum = 1
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      this.loadList()
      wx.stopPullDownRefresh()
    }, 1000);
  },
  onReachBottom() {
    this.data.pageNum += 1
    // this.loadList()
    if(this.data.currentTab == 0){
      this.loadList()
    }else{

    }
  },


})
